﻿using MaterialManagement.API.Application.Command.MesWorkOrder;
using MaterialManagement.API.Dtos;
using MaterialManagement.Domain.MesWorkOrder;
using MaterialManagement.ErrorCode;
using MaterialManagement.Infrastructure.Interface;
using MediatR;

namespace MaterialManagement.API.Application.Handler.MesWorkOrder
{
    public class GetCodeBOMHandler : IRequestHandler<GetCodeBOMCommand, APIResult<BOMInformationModel>>
    {
        private readonly IBaseRepository<BOMInformationModel> bomInfoRepo;

        public GetCodeBOMHandler(IBaseRepository<BOMInformationModel> bomInfoRepo)
        {
            this.bomInfoRepo = bomInfoRepo;
        }
        public Task<APIResult<BOMInformationModel>> Handle(GetCodeBOMCommand request, CancellationToken cancellationToken)
        {
            APIResult<BOMInformationModel> result = new APIResult<BOMInformationModel>();
            var bom = bomInfoRepo.GetAll().FirstOrDefault(x => x.BOMCode == request.BOMCode);
            if (bom == null)
            {
                result.Message = "找不到此BOM信息";
                result.Code = APIEnum.Fail;
                return Task.FromResult(result);
            }
            result.Message = "查询成功";
            result.Data = bom;
            result.Code = APIEnum.Ok;
            return Task.FromResult(result);
        }
    }
}
